//
//  BindingBankCardTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BindingBankCardTableViewController.h"
#import "LogInPassWordFooterView.h"
#import "BindingBankCardTableViewCell.h"
#import "GetPhoneNumber.h"
#import "SelectedBackName.h"

@interface BindingBankCardTableViewController ()<didClickBindingBankCardButtonDelegate,LogInPassWordFooterDelegate,SelectBankViewDelegate>

@property (nonatomic,strong)NSArray *leftArray;

@property (nonatomic,strong)NSArray *rightArray;

@property (nonatomic,strong)NSMutableArray *textFieldMutableArray;

@property (nonatomic,strong)SelectedBackName *BankNameView;

@end

static NSString *const reuseIdentifier = @"reuseIdentifier";

@implementation BindingBankCardTableViewController

- (NSMutableArray *)textFieldMutableArray {
    if (!_textFieldMutableArray) {
        _textFieldMutableArray = [NSMutableArray array];
    }
    return _textFieldMutableArray;
}

-(NSArray *)leftArray {
    if (!_leftArray) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"BindingBankCardLeft" ofType:@"plist"];
        _leftArray = [NSArray arrayWithContentsOfFile:path];
        
    }
    return _leftArray;
}

- (NSArray *)rightArray {
    if (!_rightArray) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"BindingBankCardRight" ofType:@"plist"];
        _rightArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _rightArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"绑定银行卡";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -25, 0, 0)];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BindingBankCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (!cell) {
        cell = [[BindingBankCardTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.leftLabel.text = self.leftArray[indexPath.row];
    cell.rightTextField.placeholder = self.rightArray[indexPath.row];
    
    if (indexPath.row != 2) {
        cell.rightTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    
    if (indexPath.row == 1) {
//        cell.rightTextField.userInteractionEnabled = YES;
        cell.rightTextField.enabled = NO;
        UIButton *button = [[UIButton alloc] init];
        [button addTarget:self action:@selector(didClickBankTextField:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.rightTextField.mas_left);
            make.right.equalTo(cell.rightTextField.mas_right);
            make.top.equalTo(cell.rightTextField.mas_top);
            make.bottom.equalTo(cell.rightTextField.mas_bottom);
        }];
    }
    
    [self.textFieldMutableArray addObject:cell.rightTextField];
    
    return cell;
}

- (void)didClickBankTextField:(UIButton *)button {
    [self clearResignFirstResponder];
    
    self.BankNameView = [[SelectedBackName alloc] initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT)];
    self.BankNameView.delegate = self;
    [self.view addSubview:self.BankNameView];
    [self.BankNameView showPickerView];
}

- (void)SelectCityViewCityStr:(NSString *)BankName {
    NSLog(@"%@",BankName);
    UITextField *textField = self.textFieldMutableArray[1];
    textField.text = BankName;
}

- (void)SelectCityViewCancle {
    [self.BankNameView dismissPickerView];
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return HEIGHT/5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    LogInPassWordFooterView *footer = [[LogInPassWordFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
    footer.delegate = self;
    return footer;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self clearResignFirstResponder];
}

- (void)clearResignFirstResponder {
    for (UITextField *textField in self.textFieldMutableArray) {
        [textField resignFirstResponder];
    }
}

- (void)didClickBindingBnakCardButton:(UIButton *)button {
    UITextField *phoneNumber = self.textFieldMutableArray[1];
    
    [[GetPhoneNumber SharedGetPhoneNumber] getFotgetPhoneVerifyNumber:button andPhoneNumber:phoneNumber.text andView:self.view andGetVerifyType:@"1"];
}

- (void)LogInPassWordFooterViewButton:(UIButton *)button {
    [self clearResignFirstResponder];
    
    UITextField *bankNumber = self.textFieldMutableArray.firstObject;
    
    UITextField *phoneNumber = self.textFieldMutableArray[1];
    
    UITextField *realName = self.textFieldMutableArray[2];
    
    if ((bankNumber.text.length == 0) || (phoneNumber.text.length == 0) || (realName.text.length == 0)) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
        return;
    }
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"username":USERNAME} AndMenthodName:@"api/User/info" completeBlock:^(id object) {
        
        if (JudgeLogInData) {
            NSDictionary *dict = [object objectForKey:@"data"];
            
            NSString *oilCardString = [NSString stringWithFormat:@"%@",[dict objectForKey:@"zhanghao"]];
            
            if (oilCardString.length) {
                
                MBProgressHUD *hud = [MBProgressHUD showMessage:@"您已绑定了银行卡" toView:self.view dimBackground:NO AndCompleteBlock:nil];
                [self.view addSubview:hud];
                
            } else {
                
                NSDictionary *params = @{
                                         @"username":USERNAME,
                                         @"cardNo":bankNumber.text,
                                         @"bank":phoneNumber.text,
                                         @"bank_account":realName.text
                                         };
                
                NSLog(@"%@",params);
                
                [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/User/setBankCard" completeBlock:^(id object) {
                    NSLog(@"%@",object);
                    
                    [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"绑定银行卡成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                    
                }];
            }
        }
    }];
}

@end
